package struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import fachada.Fachada;

import pessoal.Endereco;
import pessoal.cliente.ClienteFiel;
import pessoal.cliente.exception.ClienteExistenteException;
import pessoal.cliente.exception.ClienteInvalidoException;
import util.CampoObrigatorioException;
import util.ErroAcessoRepositorioException;

public class ClienteDispatchAction extends DispatchAction {
	
	/*public ActionForward perform(ActionMapping mapping, ActionForm form, 
	 	HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("listar");
	}*/
		
	public ActionForward formCliente(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("formCliente");
	}
	
	public ActionForward formAlterar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("formAlterar");
	}
	
	public ActionForward detalhar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		ClienteFiel c = null;
		try {
			c = Fachada.obterInstancia().consultarClienteFiel(request.getParameter("cpf"));
			request.setAttribute("nome", c.getNome());
			request.setAttribute("rg", c.getRg());
			request.setAttribute("desconto", c.getDesconto());
			request.setAttribute("bairro", c.getEndereco().getBairro());
			request.setAttribute("cep", c.getEndereco().getCep());
			request.setAttribute("cidade", c.getEndereco().getCidade());
			request.setAttribute("complemento", c.getEndereco().getComplemento());
			request.setAttribute("numero", c.getEndereco().getNumero());
			request.setAttribute("rua", c.getEndereco().getRua());
			request.setAttribute("uf", c.getEndereco().getUf());
		} catch (ClienteInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("formDetalhar");
	}
	
	public ActionForward listar(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("listar");
	}
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		String nome = request.getParameter("nome");
		String cpf = request.getParameter("cpf");
		String rg = request.getParameter("rg");
		double desconto = Double.parseDouble(request.getParameter("desconto"));
		String rua = request.getParameter("rua");
		String numero = request.getParameter("numero");
		String complemento = request.getParameter("complemento");
		String bairro = request.getParameter("bairro");
		String cidade = request.getParameter("cidade");
		String cep = request.getParameter("cep");
		String uf = request.getParameter("uf");
		try {
			Fachada.obterInstancia().atualizarClienteFiel(new ClienteFiel(cpf,nome,
				rg, new Endereco(rua,numero,complemento,bairro,cidade,cep,uf),desconto));
		} catch (ClienteInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		}
		
		return mapping.findForward("listar");
	}
	
	public ActionForward remover(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		try {
			Fachada.obterInstancia().excluirClienteFiel(request.getParameter("cpf"));
		} catch (ClienteInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("listar");
	}
	
	public ActionForward inserir(ActionMapping mapping,	ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		
		String nome = request.getParameter("nome");
		String cpf = request.getParameter("cpf");
		String rg = request.getParameter("rg");
		double desconto = Double.parseDouble(request.getParameter("desconto"));
		String rua = request.getParameter("rua");
		String numero = request.getParameter("numero");
		String complemento = request.getParameter("complemento");
		String bairro = request.getParameter("bairro");
		String cidade = request.getParameter("cidade");
		String cep = request.getParameter("cep");
		String uf = request.getParameter("uf");
		
		try {
			Fachada.obterInstancia().inserirClienteFiel(new ClienteFiel(cpf,nome,rg,
					new Endereco(rua,numero,complemento,bairro,cidade,cep,uf),desconto));
		} catch (ClienteExistenteException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (CampoObrigatorioException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		}
		
		return mapping.findForward("listar");
	}
}
